import { login } from "@/api/login";
import Cookies from "js-cookie";

const state = {
    nick: Cookies.get('user_nick'),
    avatar: Cookies.get('user_avatar'),
    userId: Cookies.get('user_userId'),
    introduce: Cookies.get('user_introduce'),
}

const mutations = {
    SET_USER: (state, res) => {
        state.userId = res.userId;
        state.nick = res.nick;
        state.avatar = res.avatar;
        state.introduce = res.introduce;
    },
}

const actions = {
    async Login({ commit }, userInfo) {
        const { username, password, code, uuid } = userInfo;
        const res = await login(username, password, code, uuid);
        Cookies.set('user_id', res.data.userId);
        Cookies.set('user_nick', res.data.nick);
        Cookies.set('user_avatar', res.data.avatar);
        Cookies.set('user_introduce', res.data.introduce);
        commit('SET_USER', res.data);
    },

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}